#!/usr/bin/python3
# -*- coding:utf-8 -*-

# ~Authors: chenwenke

from api import models
from rest_framework import serializers



class CoursesClassifySerializer(serializers.ModelSerializer):

    class Meta:

        model = models.CourseClassify

        fields = "__all__"

class CourseSerializer(serializers.ModelSerializer):

    class Meta:
        model = models.Course
        fields = "__all__"

    id = serializers.IntegerField()
    course_type = serializers.CharField(source="get_course_type_display")  # choice序列化
    course_level = serializers.CharField(source="get_course_level_display")
    complete_numbers = serializers.SerializerMethodField()
    def get_complete_numbers(self,obj):
        return len(models.CourseSubCategory.objects.filter(category_id=obj.id).values())
    teacher = serializers.SerializerMethodField()
    def get_teacher(self,obj):
        temp = []
        for item in obj.teacher.all():
            temp.append(item.name)
            temp.append(item.teacher_position)
        result = " ".join(temp)
        return result

    price_type = serializers.CharField(source="price.get_price_type_display")
    cost_price = serializers.FloatField(source="price.cost_price")
    now_price = serializers.FloatField(source="price.now_price")
    course_hour = serializers.CharField(source="coursedetail.course_hour")

    category = serializers.SerializerMethodField()

    def get_category(self,obj):
        temp = []
        for item in models.CourseSubCategory.objects.filter(category_id=obj.id).values()[:4]:
            temp.append(item)
        return temp


class CourseDetailSerializer(serializers.ModelSerializer):

    class Meta:

        model = models.Course
        fields = ["title","course_img","popularity",'course_level','course_hour','hour','promotion_end_date',"price_type",
                  "cost_price","now_price"]

    course_level = serializers.CharField(source="get_course_level_display")
    course_hour = serializers.CharField(source="coursedetail.course_hour")
    hour = serializers.CharField(source="coursedetail.hour")
    promotion_end_date = serializers.CharField(source="coursedetail.promotion_end_date")
    price_type = serializers.CharField(source="price.get_price_type_display")
    cost_price = serializers.FloatField(source="price.cost_price")
    now_price = serializers.FloatField(source="price.now_price")